#include "weather.h"

WeatherInfo current_weather;

void weather_Init()
{
  current_weather.city = "未知";
  current_weather.weather = "未知";
  current_weather.hightemp = "0";
  current_weather.lowtemp = "0";
}

bool weather_update()
{
  //构建API请求URL
  char url[200];

  sprintf(url, WEATHER_URL, WEATHER_API_KEY, WEATHER_CITY_CODE);

  WiFiClient client;
  HTTPClient http;

  if(http.begin(client, url))
  {
    int httpcode = http.GET();

    if(httpcode == HTTP_CODE_OK)
    {
      String payload = http.getString();
      DynamicJsonDocument doc(1024);
      DeserializationError error = deserializeJson(doc, payload);

      if(!error)
      {
        JsonObject result_0 = doc["results"][0];  
        JsonObject location = result_0["location"];  
        JsonArray daily = result_0["daily"];
        JsonObject today = daily[0];

        if(!today.isNull())
        {
          current_weather.city = location["name"].as<String>();
          current_weather.weather = today["code_day"].as<String>();
          current_weather.hightemp = today["high"].as<String>();
          current_weather.lowtemp = today["low"].as<String>();

          http.end();
          return true;
        }
      }
    }
    http.end();
  }
  return false;
}

void get_weatherInfo()
{
  static String weather = "100";
  weather_update();
  
  if(weather != current_weather.weather)  //刷新
  {
    OLED_Clear();
    weather = current_weather.weather;
  }
  
  if(current_weather.weather == "4")      //多云
  {
    OLED_ShowChinese(0, 4, 4, 16);
    OLED_ShowChinese(17, 4, 5, 16);
  }
  else if(current_weather.weather == "0") //晴
  {
    OLED_ShowChinese(0, 4, 2, 16);
  }
  else if(current_weather.weather == "5") //晴间多云
  {
    OLED_ShowChinese(0,4, 2, 16);
    OLED_ShowChinese(17, 4, 3, 16);
    OLED_ShowChinese(34, 4, 4, 16);
    OLED_ShowChinese(51, 4, 5, 16);
  }
  else if(current_weather.weather == "7") //大部多云
  {
    OLED_ShowChinese(0, 4, 6, 16);
    OLED_ShowChinese(17, 4, 7, 16);
    OLED_ShowChinese(34, 4, 4, 16);
    OLED_ShowChinese(51, 4, 5, 16);
  }
  else if(current_weather.weather == "9") //阴
  {
    OLED_ShowChinese(0, 4, 8, 16);
  }
  else if(current_weather.weather == "10") //阵雨
  {
    OLED_ShowChinese(0, 4, 10, 16);
    OLED_ShowChinese(17, 4, 11, 16);
  }
  else if(current_weather.weather == "11") //雷阵雨
  {
    OLED_ShowChinese(0, 4, 9, 16);
    OLED_ShowChinese(17, 4, 10, 16);
    OLED_ShowChinese(34, 4, 11, 16);
  }
  else if(current_weather.weather == "13") //小雨
  {
    OLED_ShowChinese(0, 4, 12, 16);
    OLED_ShowChinese(17, 4, 11, 16);
  }
  else if(current_weather.weather == "14") //中雨
  {
    OLED_ShowChinese(0, 4, 13, 16);
    OLED_ShowChinese(17, 4, 11, 16);
  }
  else if(current_weather.weather == "15") //大雨
  {
    OLED_ShowChinese(0, 4, 6, 16);
    OLED_ShowChinese(17, 4, 11, 16);
  }
  else if(current_weather.weather == "16") //暴雨
  {
    OLED_ShowChinese(0, 4, 14, 16);
    OLED_ShowChinese(17, 4, 11, 16);
  }
  else if(current_weather.weather == "17") //大暴雨
  {
    OLED_ShowChinese(0, 4, 6, 16);
    OLED_ShowChinese(17, 4, 14, 16);
    OLED_ShowChinese(34, 4, 11, 16);
  }

  else if(current_weather.weather == "18") //特大暴雨
  {
    OLED_ShowChinese(0, 4, 15, 16);
    OLED_ShowChinese(17, 4, 6, 16);
    OLED_ShowChinese(34, 4, 14, 16);
    OLED_ShowChinese(51, 4, 11, 16);
  }
  else if(current_weather.weather == "20") //雨夹雪
  {
    OLED_ShowChinese(0, 4, 11, 16);
    OLED_ShowChinese(17, 4, 16, 16);
    OLED_ShowChinese(34, 4, 17, 16);
  }
  else if(current_weather.weather == "21") //阵雪
  {
    OLED_ShowChinese(0, 4, 10, 16);
    OLED_ShowChinese(17, 4, 17, 16);
  }
  else if(current_weather.weather == "22") //小雪
  {
    OLED_ShowChinese(0, 4, 12, 16);
    OLED_ShowChinese(17, 4, 17, 16);
  }
  else if(current_weather.weather == "23") //中雪
  {
    OLED_ShowChinese(0, 4, 13, 16);
    OLED_ShowChinese(17, 4, 17, 16);
  }
  else if(current_weather.weather == "24") //大雪
  {
    OLED_ShowChinese(0, 4, 6, 16);
    OLED_ShowChinese(17, 4, 17, 16);
  }
  else if(current_weather.weather == "30") //雾
  {
    OLED_ShowChinese(0, 4, 18, 16);
  }
  else if(current_weather.weather == "31") //霾
  {
    OLED_ShowChinese(0, 4, 19, 16);
  }

  
  OLED_ShowString(68, 4, current_weather.lowtemp.c_str(), 16);
  OLED_ShowString(85, 4, "~", 16);
  OLED_ShowString(102, 4, current_weather.hightemp.c_str(), 16);
}
